package com.example.livebroadcast.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.livebroadcast.model.UserRedis;
import com.example.livebroadcast.service.LiveService;
import com.example.livebroadcast.service.UserService;

@RestController
public class RegisterController {
	@Autowired
	private UserService userService;

	@Autowired
	private LiveService liveService;

	@RequestMapping(value = "/register")
	public ModelAndView toRegister(ModelMap modelMap) {
		modelMap.addAttribute("user", new UserRedis());
		return new ModelAndView("user/register");
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ModelAndView doRegister(@ModelAttribute(value = "user") UserRedis userRedis, HttpSession session,
			ModelMap modelMap) {
		userService.save(userRedis);
		session.setAttribute("user", userRedis);
		modelMap.addAttribute("liveList", liveService.getAll());
		return new ModelAndView("user/liveList");
	}
}
